package com.shjn.squirrel.model;

import android.content.Context;
import android.content.SharedPreferences;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.shjn.squirrel.bean.httpBean.ParamBean;
import com.shjn.squirrel.bean.storageBean.BottleState;
import com.shjn.squirrel.bean.storageBean.Location;
import com.shjn.squirrel.bean.storageBean.WastePrice;
import com.shjn.squirrel.bean.storageBean.WorkSetting;

import java.lang.reflect.Type;
import java.util.ArrayList;

/**
 * 本地配置文件存储（相关参数）
 */
public class LocalStorage {

    private LocalStorage() {
    }

    /**
     * 出厂设置信息
     */
    private static final String FactorySetting_File_Name = "factorySettingPref";
    private static final Object factorySettingLock = new Object();//出厂设置配置文件锁对象

    private static final String DeviceID_Key = "deviceID";//设备ID的key
    private static final String Location_Key = "location";//设备经纬度信息的key

    /**
     * 回收机工作参数
     */
    private static final String WorkSetting_File_Name = "workSettingPref";
    private static final Object workSettingLock = new Object();//回收机工作参数配置文件锁对象

    private static final String WorkSetting_Key = "workSetting";//回收机工作参数的key

    /**
     * 页面资源信息
     */
    private static final String PageResource_File_Name = "pageResourcePref";
    private static final Object pageResourceLock = new Object();//页面资源信息配置文件锁对象

    private static final String WastePrice_Key = "wastePrice";//垃圾价格的key

    /**
     * 垃圾分类桶状态
     */
    private static final String CanState_File_Name = "canStatePref";
    private static final Object canStateLock = new Object();//垃圾分类桶状态配置文件锁对象

    private static final String CanState_Key = "canState";//垃圾分类桶状态的key
    private static final String BottleState_Key = "bottleState";//饮料瓶分类桶状态的key

    /**
     * 保存设备ID
     *
     * @param context
     * @param value
     */
    public static void saveDeviceID(Context context, String value) {
        synchronized (factorySettingLock) {
            SharedPreferences sp = context.getSharedPreferences(FactorySetting_File_Name, Context.MODE_PRIVATE);
            sp.edit().putString(DeviceID_Key, value).apply();
        }
    }

    /**
     * 获取设备ID
     *
     * @param context
     * @return
     */
    public static String getDeviceID(Context context) {
        synchronized (factorySettingLock) {
            SharedPreferences sp = context.getSharedPreferences(FactorySetting_File_Name, Context.MODE_PRIVATE);
            return sp.getString(DeviceID_Key, "");
        }
    }

    /**
     * 保存设备经纬度信息
     *
     * @param context
     * @param value
     */
    public static void saveLocation(Context context, Location value) {
        synchronized (factorySettingLock) {
            SharedPreferences sp = context.getSharedPreferences(FactorySetting_File_Name, Context.MODE_PRIVATE);
            sp.edit().putString(Location_Key, new Gson().toJson(value)).apply();
        }
    }

    /**
     * 获取设备经纬度信息
     *
     * @param context
     * @return
     */
    public static Location getLocation(Context context) {
        synchronized (factorySettingLock) {
            SharedPreferences sp = context.getSharedPreferences(FactorySetting_File_Name, Context.MODE_PRIVATE);
            String jsonStr = sp.getString(Location_Key, "");
            if (jsonStr.isEmpty()) {
                return new Location();
            }
            return new Gson().fromJson(jsonStr, Location.class);
        }
    }

    /**
     * 保存回收机工作参数
     *
     * @param context
     * @param value
     */
    public static void saveWorkSetting(Context context, WorkSetting value) {
        synchronized (workSettingLock) {
            SharedPreferences sp = context.getSharedPreferences(WorkSetting_File_Name, Context.MODE_PRIVATE);
            sp.edit().putString(WorkSetting_Key, new Gson().toJson(value)).apply();
        }
    }

    /**
     * 获取回收机工作参数
     *
     * @param context
     * @return
     */
    public static WorkSetting getWorkSetting(Context context) {
        synchronized (workSettingLock) {
            SharedPreferences sp = context.getSharedPreferences(WorkSetting_File_Name, Context.MODE_PRIVATE);
            String jsonStr = sp.getString(WorkSetting_Key, "");
            if (jsonStr.isEmpty()) {
                return new WorkSetting();
            }
            return new Gson().fromJson(jsonStr, WorkSetting.class);
        }
    }

    /**
     * 保存垃圾价格
     *
     * @param context
     * @param value
     */
    public static void saveWastePrice(Context context, ArrayList<WastePrice> value) {
        synchronized (pageResourceLock) {
            SharedPreferences sp = context.getSharedPreferences(PageResource_File_Name, Context.MODE_PRIVATE);
            sp.edit().putString(WastePrice_Key, new Gson().toJson(value)).apply();
        }
    }

    /**
     * 获取垃圾价格
     *
     * @param context
     * @return
     */
    public static ArrayList<WastePrice> getWastePrice(Context context) {
        synchronized (pageResourceLock) {
            SharedPreferences sp = context.getSharedPreferences(PageResource_File_Name, Context.MODE_PRIVATE);
            String jsonStr = sp.getString(WastePrice_Key, "");
            if (jsonStr.isEmpty()) {
                ArrayList<WastePrice> wastePriceList = new ArrayList<>();
                for (int i = 0; i < 8; i++) {
                    wastePriceList.add(new WastePrice(i + 1, 0, 0));
                }
                return wastePriceList;
            }
            Type listType = new TypeToken<ArrayList<WastePrice>>() {
            }.getType();
            return new Gson().fromJson(jsonStr, listType);
        }
    }

    /**
     * 保存垃圾桶状态参数
     *
     * @param context
     * @param value
     */
    public static void saveCanState(Context context, ArrayList<ParamBean> value) {
        synchronized (canStateLock) {
            SharedPreferences sp = context.getSharedPreferences(CanState_File_Name, Context.MODE_PRIVATE);
            sp.edit().putString(CanState_Key, new Gson().toJson(value)).apply();
        }
    }

    /**
     * 获取垃圾桶状态参数
     *
     * @param context
     * @return
     */
    public static ArrayList<ParamBean> getCanState(Context context) {
        synchronized (canStateLock) {
            SharedPreferences sp = context.getSharedPreferences(CanState_File_Name, Context.MODE_PRIVATE);
            String jsonStr = sp.getString(CanState_Key, "");
            if (jsonStr.isEmpty()) {
                ArrayList<ParamBean> canParamList = new ArrayList<>();
                for (int i = 0; i < 8; i++) {
                    canParamList.add(new ParamBean(i + 1, 0, 0, 0, 0));
                }
                return canParamList;
            }
            Type listType = new TypeToken<ArrayList<ParamBean>>() {
            }.getType();
            return new Gson().fromJson(jsonStr, listType);
        }
    }

    /**
     * 保存饮料瓶分类桶状态
     *
     * @param context
     * @param value
     */
    public static void saveBottleState(Context context, BottleState value) {
        synchronized (canStateLock) {
            SharedPreferences sp = context.getSharedPreferences(CanState_File_Name, Context.MODE_PRIVATE);
            sp.edit().putString(BottleState_Key, new Gson().toJson(value)).apply();
        }
    }

    /**
     * 获取饮料瓶分类桶状态
     *
     * @param context
     * @return
     */
    public static BottleState getBottleState(Context context) {
        synchronized (canStateLock) {
            SharedPreferences sp = context.getSharedPreferences(CanState_File_Name, Context.MODE_PRIVATE);
            String jsonStr = sp.getString(BottleState_Key, "");
            if (jsonStr.isEmpty()) {
                return new BottleState();
            }
            return new Gson().fromJson(jsonStr, BottleState.class);
        }
    }
}
